package com.kirk.cts.core.base.pagination.util;

import java.sql.*;

/**
 * <p> DB.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-08-31 23:08
 * @Description
 */
public class DB {
    Connection con;
    Statement stt;

    private void begin() throws ClassNotFoundException, SQLException {
        if (con != null || stt != null) {
            this.closs();
        }
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://27.54.193.108:3306/meishih", "meishih", "duan1982");
        stt = con.createStatement();

    }


    public String cun(String sql) throws ClassNotFoundException, SQLException {
        this.begin();
        DatabaseMetaData dbmd = con.getMetaData();
        ResultSet tSet = dbmd.getTables(null, "%", "%", new String[]{"TABLE", "VIEW"});
        StringBuilder tempInfo = new StringBuilder();
        tempInfo.append("数据库产品名: " + dbmd.getDatabaseProductName()).append("<br />");
        tempInfo.append("数据库是否支持事务: " + dbmd.supportsTransactions()).append("<br />");
        tempInfo.append("数据库产品的版本号:" + dbmd.getDatabaseProductVersion()).append("<br />");
        while (tSet.next()) {
            tempInfo.append(tSet.getRow() + "==表类别:" + tSet.getString("TABLE_CAT")).append("<br />");
            tempInfo.append(tSet.getRow() + "==表模式:" + tSet.getString("TABLE_SCHEM")).append("<br />");
            tempInfo.append(tSet.getRow() + "==表名称:" + tSet.getString("TABLE_NAME")).append("<br />");
        }
        tSet.close();
        return tempInfo.toString();
    }

    public void closs() throws SQLException {
        stt.close();
        con.close();
    }

    public static void main(String[] args) {
        DB a = new DB();
        try {
            String b = a.cun("select * from crawler_rule");
            System.out.println(b);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
